package com.itbaizhan.shopping_manager_api.controller;

import com.itbaizhan.shopping_common.result.BaseResult;
import com.itbaizhan.shopping_common.service.FileService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.spring.context.annotation.DubboConfigConfiguration;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * 文件上传控制层
 */
@RestController
@RequestMapping("/file")
public class FileController {
	
	@DubboReference
	private FileService fileService;
	
	/**
	 * 文件上传
	 * @param file
	 * @return
	 * @throws IOException
	 */
	@PostMapping("/uploadImage")
	public BaseResult uploadImage(MultipartFile file)throws IOException {
		byte[] fileBytes = file.getBytes();
		String fileName = file.getOriginalFilename();
		String filePath = fileService.uploadImage(fileBytes, fileName);
		return BaseResult.ok(filePath);
	}
	
	/**
	 * 删除文件
	 * @param filePath
	 * @return
	 */
	@DeleteMapping("/delete")
	public BaseResult delete(String filePath){
		fileService.delete(filePath);
		return BaseResult.ok();
	}
	
	
}
